﻿using System;
using System.Collections.Generic;

using Microsoft.Practices.Unity;

using Fuse8.DomainFramework.Common.Locator;

namespace Fuse8.DomainFramework.UnityLocator
{
    /// <summary>
    /// 
    /// </summary>
    public class UnityServiceLocatorWithLazy : ServiceLocatorBaseEx, IDisposable
    {
        private UnityServiceLocatorWrapper _unityServiceLocatorWrapper;
        private bool _isDisposed = false;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="container"></param>
        public UnityServiceLocatorWithLazy(IUnityContainer container)
        {
            _unityServiceLocatorWrapper = new UnityServiceLocatorWrapper(container);
        }

        #region ServiceLocatorBase override

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="service"></param>
        public override void Register<TService>(TService service)
        {
            _unityServiceLocatorWrapper.Register(service);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <param name="service"></param>
        public override void Register<TService>(string key, TService service)
        {
            _unityServiceLocatorWrapper.Register(key, service);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="typeTo"></param>
        /// <param name="serviceType"></param>
        public override void Register(Type typeTo, Type serviceType)
        {
            _unityServiceLocatorWrapper.Register(typeTo, serviceType);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="typeTo"></param>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        public override void Register(Type typeTo, Type serviceType, string key)
        {
            _unityServiceLocatorWrapper.Register(typeTo, serviceType, key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public override bool IsRegistered<TService>()
        {
            return _unityServiceLocatorWrapper.IsRegistered<TService>();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public override bool IsRegistered<TService>(string key)
        {
            return _unityServiceLocatorWrapper.IsRegistered<TService>(key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public override bool IsRegistered(Type serviceType)
        {
            return _unityServiceLocatorWrapper.IsRegistered(serviceType);
        }

        public override bool IsRegistered(Type serviceType, string key)
        {
            return _unityServiceLocatorWrapper.IsRegistered(serviceType, key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="service"></param>
        public override void SetDefaultService<TService>(TService service)
        {
            _unityServiceLocatorWrapper.SetDefaultService<TService>(service);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        public override void SetDefaultService(Type serviceType)
        {
            _unityServiceLocatorWrapper.SetDefaultService(serviceType);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public override TService GetDefaultService<TService>()
        {
            return _unityServiceLocatorWrapper.GetDefaultService<TService>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public override object GetDefaultService(Type serviceType)
        {
            return _unityServiceLocatorWrapper.GetDefaultService(serviceType);
        }

        /// <summary>
        /// Check if an instance of TService is registered in IoC as default service
        /// </summary>
        /// <typeparam name="TService">Type of instance</typeparam>
        /// <returns>True if registered, false if not</returns>
        public override bool IsDefaultServiceRegistered<TService>()
        {
            return _unityServiceLocatorWrapper.IsDefaultServiceRegistered<TService>();
        }

        /// <summary>
        /// Check if a type is registered in IoC as a type for default service
        /// </summary>
        /// <param name="serviceType">Type of registration</param>
        /// <returns>True if registered, false if not</returns>
        public override bool IsDefaultServiceRegistered(Type serviceType)
        {
            return _unityServiceLocatorWrapper.IsDefaultServiceRegistered(serviceType);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
        {
            return _unityServiceLocatorWrapper.CallDoGetAllInstances(serviceType);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        protected override object DoGetInstance(Type serviceType, string key)
        {
            return _unityServiceLocatorWrapper.CallDoGetInstance(serviceType, key);
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            GC.SuppressFinalize(this);   
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    if (_unityServiceLocatorWrapper != null)
                    {
                        _unityServiceLocatorWrapper.Dispose();
                        _unityServiceLocatorWrapper = null;
                    }
                }
            }

            _isDisposed = true;
        }        

        #endregion        
    }

    internal class UnityServiceLocatorWrapper : UnityServiceLocatorEx
    {
        internal UnityServiceLocatorWrapper(IUnityContainer container)
            : base(container)
        { }

        internal object CallDoGetInstance(Type serviceType, string key)
        {
            return base.DoGetInstance(serviceType, key);
        }

        internal IEnumerable<object> CallDoGetAllInstances(Type serviceType)
        {
            return base.DoGetAllInstances(serviceType);
        }
    }
}
